define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sum/richtext/index' + location.search,
                    add_url: 'sum/richtext/add',
                    edit_url: 'sum/richtext/edit',
                    del_url: 'sum/richtext/del',
                    multi_url: 'sum/richtext/multi',
                    import_url: 'sum/richtext/import',
                    table: 'sum_richtext',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('Type'), searchList: Config.typeList, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'sum/richtext/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'sum/richtext/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'sum/richtext/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sum/richtext/index',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // sortName: 'id',
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showExport: false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'type', title: __('Type'), searchList: {"normal":__('Normal'),"system":__('System'),"activity":__('Activity'),"tip":__('Tip')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    var multiple = Backend.api.query('multiple');
                                    multiple = multiple == 'true' ? true : false;
                                    row.ids=row.id.toString()
                                    Fast.api.close({data: row, multiple: multiple});
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var couponsArr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    couponsArr.push(j.id);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;
                let row={}
                row.ids=couponsArr.join(",")
                Fast.api.close({data: row, multiple: multiple});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            require(['upload'], function (Upload) {
                Upload.api.plupload($("#toolbar .plupload"), function () {
                    $(".btn-refresh").trigger("click");
                });
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $("#baidu_trans").on('click', function (e) {
                    //来源语言
                    var from_lang=Config.default_lang;
                    //获取当前的语言
                    var lang=$("input[name='row[lang]']:checked").val();
                    if(from_lang==lang){
                        Toastr.error(__('要翻译的语言和来源语音一致'));
                        return false;
                    }
                    //获取需要翻译的值
                    var trans_fields=[{field:'title',type:'input',value:''},{field:'description',type:'textarea',value:''},{field:'content',type:'summernote',value:''}];
                    trans_fields.forEach(function (item){
                        if(item.type=="summernote"){
                            item.ftype="textarea";
                        }else{
                            item.ftype=item.type;
                        }
                        if(from_lang=='zh_cn'){
                            item.name=item.ftype+"[name='row["+item.field+"]']";
                        }else{
                            item.name=item.ftype+"[name='row[lang_data]["+from_lang+"]["+item.field+"]']";
                        }
                        item.value=$(item.name).val();
                    });
                    console.log(lang);
                    Toastr.success('翻译中...');
                    $.ajax({
                        url: 'sum/baidu_trans/dotrans',
                        dataType: 'json',
                        data: {lang: lang,fields:JSON.stringify(trans_fields)},
                        cache: false,
                        success: function (ret) {
                            var ret_fields=ret.data.fields;
                            ret_fields.forEach(function (item){
                                if(lang=='zh_cn'){
                                    item.tname=item.ftype+"[name='row["+item.field+"]']";
                                }else{
                                    item.tname=item.ftype+"[name='row[lang_data]["+lang+"]["+item.field+"]']";
                                }
                                if(item.type=='input'){
                                    $(item.tname).val(item.value);
                                }else if(item.type=='textarea'){
                                    $(item.tname).html(item.value);
                                }else if(item.type=='summernote'){
                                    $(item.tname).summernote('pasteHTML', item.value);
                                }
                            });
                            Toastr.success('翻译成功');
                        }, error: function () {
                            Toastr.error(__('翻译失败'));
                        }
                    });
                });
            }
        }


    };
    return Controller;
});
